<?php

/*
 * this is for fetching comments
 * 1. either called via ajax, after a new comment is submtted
 * 2. or included as in by fetch_stream, in which case the variable $comments_include will be set.
 */

//getting the params

$response = array();
if(!isset($comments_include))
{

	require_once("../../connection.php");
require_once("../../../functions_shared.php");
	
$type_comments = $_POST['type'];
$user = $_POST['user']; // the presently logged in user
if(isset($_POST['offset'])) 	$offset_text = "AND `id`<".$_POST['offset']."";
else $offset_text = "";

$arr1_comments = explode("_", $type_comments);
$temp1_comments = $arr1_comments[0];
if($temp1_comments == "new")
{
	$offset_text = " AND `timestamp`> '".$arr1_comments[1]."'";
	$type_comments = str_replace("".$arr1_comments[0]."_".$arr1_comments[1]."_", "", $type_comments);
}

$response["error"] = "";
}
else {
	$offset_text = "";
}


//processing the params
$arr_comments = explode("_",$type_comments);
$type1_comments = $arr_comments[0];

if($type1_comments == "commentbox")
{
	if($arr_comments[1] == "stream")
	{
	$postid_comments = $arr_comments[3];
	$response["postid"] = $postid_comments;
	$result_comments = mysql_query("SELECT * FROM site_comments WHERE `type`='comment_post_".$postid_comments."' AND `del`!='1' ".$offset_text." ORDER BY `id` DESC");
	if(!$result_comments) $response["error"] .=  mysql_error();
	}
}


if(!isset($comments_include)) 
	$output = "";

$size_comments = mysql_numrows($result_comments);

// option to view more
if($size_comments>4) {
$last_id_comments = mysql_result($result_comments, 3, 'id');
	$output=$output.'<div class="a comment_more comment" style="" onclick=\'init_comments("'.$user.'", "'.$type_comments.'",'.$last_id_comments.')\' id="'.$type_comments.'_more">'.($size_comments-4).' more</div>';
}

//$output = $output.'<br>';
//actual stream
$counter_comments = 0;
$output_temp1 = "";
for($i=0;$i<$size_comments&&$counter_comments<4;$i++)
{
	$output_temp = "";
	$name = mysql_result($result_comments, $i, 'postas');
	$temp_name = explode("_",$name);
	if($temp_name[0] == "name")
		$name = $temp_name[1];
	
	$timestamp = mysql_result($result_comments, $i, 'timestamp');
	$out = mysql_result($result_comments, $i, 'text');
	$fbid = mysql_result($result_comments, $i, 'user');
	$row_id = mysql_result($result_comments, $i, 'id'); 
	
	//output starts
		$output_temp=$output_temp.'<div class="comment" id="comment_'.$row_id.'">';		
		$out = stripslashes($out);
		if($out!="")  $output_temp=$output_temp.convertUrl($out);
		$output_temp=$output_temp.' - <span class="small-font faded"><strong>'.$name.'</strong>&nbsp;<span class=" 	time_stream" id="time_comment_'.$row_id.'">'.process_time($timestamp).'</span>';
		//option to delete
		if($user == mysql_result($result_comments, $i, 'user'))
	$output_temp=$output_temp.'&nbsp;<span class="faded"><span class="a" onclick=\'del_post('.$row_id.',this,"init","comments")\'>delete</span></span></span>';
		$output_temp=$output_temp.'</div>';
		
		$counter_comments++;
		$output_temp1 = $output_temp.$output_temp1;  
	
}

$output = $output.$output_temp1;
$time = time();
if($size_comments!=0)
$output = $output.'<div class="comments_new" id="new_'.$time.'_'.$type_comments.'"></div>';

if(!isset($comments_include))
{
	$response['output'] = $output;
	echo json_encode($response);
}

?>
